package com.mt.api.modules.service.impl;


import com.mt.api.modules.domain.BossPrinter;
import com.mt.api.modules.domain.bo.PrinterUpdateBo;
import com.mt.api.modules.manager.IBossPrinterManager;
import com.mt.api.modules.mapstruct.BossPrinterMapStruct;
import com.mt.api.modules.service.IBossPrinterService;
import com.mt.api.util.JwtUserUtils;
import com.mt.common.response.WebResponse;
import com.mt.common.response.WebResponseUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Objects;


/**
 * @Author: plover
 * @Date: 2022-02-12
 * @Description:
 */
@Service
@RequiredArgsConstructor
public class BossPrinterServiceImpl implements IBossPrinterService {

    private final IBossPrinterManager bossPrinterManager;
    private final BossPrinterMapStruct bossPrinterMapStruct;


    @Override
    public WebResponse updateBossPrinter(PrinterUpdateBo printerUpdateBo) {
        Integer bossId = JwtUserUtils.getJwtUser().getAppBoss().getId();

        BossPrinter dbBossPrinter = bossPrinterManager.getByParam(printerUpdateBo);
        BossPrinter bossPrinter = bossPrinterMapStruct.boToEntity(printerUpdateBo);
        bossPrinter.setBossId(bossId);
        if (Objects.isNull(dbBossPrinter)) {
            bossPrinterManager.save(bossPrinter);
        } else {
            bossPrinter.setId(dbBossPrinter.getId());
            bossPrinterManager.updateById(bossPrinter);
        }
        return WebResponseUtils.success();
    }
}
